# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

####注意一定要导入配置，因为数据库的一些连接信息写在settings文件里的
# 此类是把信息写入文档，写入时末尾都加了一个逗号，是为了数据的观看与直观性
# 也方便以后用mysql语言直接导入数据
from scrapy.conf import settings


# class GamerankPipeline(object):
#     def process_item(self, item, spider):
#         with open('games_list.txt','a',encoding='utf-8') as f:
#         	f.write(item['rank']+',')
#         	f.write(item['game']+',')
#         	f.write(item['type']+',')
#         	f.write(item['status']+',')
#         	f.write(item['hot'])
#         	f.write('\n')


# 下面是将爬取到的信息插入到MySQL数据库中
class Gamerank_Pipeline(object):
    def process_item(self, item, spider):
        host = settings['MYSQL_HOSTS']
        user = settings['MYSQL_USER']
        psd = settings['MYSQL_PASSWORD']
        db = settings['MYSQL_DB']
        c = settings['CHARSET']
        port = settings['MYSQL_PORT']
        # 数据库连接
        con = pymysql.connect(host=host, user=user, passwd=psd, db=db, charset=c, port=port)
        # 数据库游标
        cue = con.cursor()
        print("mysql connect succes")  # 测试语句，这在程序执行时非常有效的理解程序是否执行到这一步
        # sql="insert into gamerank (rank,g_name,g_type,g_status,g_hot) values(%s,%s,%s,%s,%s)" % (item['rank'],item['game'],item['type'],item['status'],item['hot'])
        try:
            cue.execute("insert into gamerank (rank,g_name,g_type,g_status,g_hot) values(%s,%s,%s,%s,%s)",
                        [item['rank'], item['game'], item['type'], item['status'], item['hot']])
            print("insert success")  # 测试语句
        except Exception as e:
            print('Insert error:', e)
            con.rollback()
        else:
            con.commit()
        con.close()
        return item
